'use strict';

/**
 * Created by zhu on 14/12/26.
 */
angular.module('blog').controller('BlogEditCtrl', [
    '$scope',
    '$rootScope',
    '$location',
    'BlogCategoryQueryService',
    'PathSuffixConstant',
    'BlogNewService',
    'BlogDetailService',
    '$routeParams',
    function ($scope, $rootScope, $location, BlogCategoryQueryService, PathSuffixConstant, BlogNewService, BlogDetailService, $routeParams) {
        $rootScope.visitedUserId = $routeParams.userId;
        var blogId = $routeParams.blogId;

        $scope.contentInit = {language: 'zh_CN'};
        $scope.blog = {author: {identity: $routeParams.userId}};

        $scope.query = function() {
            BlogCategoryQueryService.query(
                {author: $scope.blog.author.identity + PathSuffixConstant},
                function (response) {
                    $scope.categories = response;

                    if (hasId(blogId)) {
                        BlogDetailService.query({blogId: blogId + PathSuffixConstant}, function(response) {
                            $scope.blog = response;
                            angular.forEach($scope.categories, function(v) {
                                if (v._id == response.categoryId) {
                                    $scope.blog.categoryId = v;
                                }
                            });
                        });
                    } else if ($scope.categories.length > 0) {
                        $scope.blog.categoryId = $scope.categories[0];
                    }
                }
            );
        };

        /**
         * 创建文章
         */
        $scope.submit = function () {
            if (!validate()) {
                return;
            }

            var blogToSave = getBlogToSave($scope.blog);
            if (hasId(blogId)) {
                blogToSave.identity = blogId;
                BlogDetailService.change({blogId: blogId}, blogToSave, function() {goDetail(blogId)});
            } else {
                BlogNewService.save(blogToSave, function (blog) {goDetail(blog._id)});
            }
        };

        function goDetail(blogId) {
            $rootScope.newBlog = $scope.blog;

            var categoryId = $rootScope.newBlog.categoryId;
            if (categoryId) {
                $rootScope.newBlog.category = {
                    _id: categoryId,
                    name: categoryName(categoryId)
                }
            }
            $location.path($routeParams.userId + '/blog/' + blogId);
        }

        function categoryName(categoryId) {
            var categoryName = '';
            angular.forEach($scope.categories, function(v) {
                if (v._id == categoryId) {
                    categoryName = v.name;
                }
            });
            return categoryName;
        }

        function getBlogToSave(blog) {
            var blogToSave = blog;
            if (blogToSave.categoryId) {
                blogToSave.categoryId = blogToSave.categoryId._id;
            }
            return blogToSave;
        }

        function validate() {
            if (!$scope.blog.title || $scope.blog.title == '') {
                alert('请输入标题！');
                return false;
            }
            if (!$scope.blog.content || $scope.blog.content == '') {
                alert('请输入内容！');
                return false;
            }
            return true;
        }

        function hasId(blogId) {
            return blogId && blogId != '';
        }

        $scope.query();

    }]);
